package com.example.myimageshare.View;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myimageshare.R;
import com.example.myimageshare.domain.User;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity {

    //组件
    private EditText et_username_rgs;
    private EditText et_password_rgs;
    private EditText et_password_again;

    private Button btn_toLogon;
    private Button btn_rigister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        initListener();
    }


    private void initView(){
        et_username_rgs = findViewById(R.id.UserNameEdit_reg);
        et_password_rgs = findViewById(R.id.PassWordEdit_rgs);
        et_password_again = findViewById(R.id.PassWordAgainEdit_reg);

        btn_rigister = findViewById(R.id.RegisterButton_rgs);
        btn_toLogon = findViewById(R.id.BackLoginButton);
    }

    private void initListener(){
        btn_rigister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(et_username_rgs.getText().toString().trim().length() < 6){
                    showMsg("用户名不能少于6位");
                    return;
                }
                if(et_username_rgs.getText().toString().trim().length()>16){
                    showMsg("用户名不能大于16位");
                    return;
                }

                if(et_password_again.getText().toString().trim().length()<6
                        ||et_password_rgs.getText().toString().trim().length()<6){
                    showMsg("用户密码不能小于6位");
                    return;
                }
                if(et_password_again.getText().toString().trim().length()>16
                        ||et_password_rgs.getText().toString().trim().length()>16){
                    showMsg("用户密码不能大于16位");
                    return;
                }
                if(!et_password_rgs.getText().toString().trim()
                        .equals(et_password_again.getText().toString().trim())){
                    showMsg("两次密码输入不一致");
                    return;
                }

                signUp();

            }
        });

        btn_toLogon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void showMsg(String msg){
        Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_SHORT).show();

    }

    /*注册*/
    private void signUp() {
        final User user = new User();
        user.setUsername(et_username_rgs.getText().toString().trim());
        user.setPassword(et_password_again.getText().toString().trim());
        user.signUp(new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null) {
                    Log.d("MainActicity","注册成功");
                    showMsg("注册成功，请切换登录");
                } else {
                    Log.d("MainActicity","注册失败");
                }
            }
        });
    }
}
